<template>
	<div>
		<!-- 选择管控措施弹窗 -->
    <el-dialog 
      title="选择管控措施"
      append-to-body
      :visible.sync="chooseMeasureDialog"
      :close-on-click-modal="false" 
      :before-close="handleCloseChooseMeasure"
      width="60%"
    >

      <!-- 表格内容 -->
      <avue-crud 
        :option="cmOption"
        :data="cmData.filter((item) => item.yhjb.includes(filterYHJB))"
        @selection-change="cmSelectionChange"
        ref="cmCrud"
      >
        <template slot="menuLeft">
          <el-select 
            placeholder="请选择隐患级别"
            v-model="filterYHJB" 
            clearable 
            size="small"
          >
            <el-option
              v-for="(item, index) in dangerTypes"
              :key="index"
              :label="item.dictValue"
              :value="item.dictKey">
            </el-option>
          </el-select>
        </template>
      </avue-crud>

      <!-- 按钮 -->
      <div class="choose_cm_footer">
        <el-button 
          type="primary"
          size="small"
          @click="chooseControlMeasure()">
          确 认
        </el-button>
        <el-button 
          size="small"
          @click="cancelChooseControlMeasure()">
          取 消
        </el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
import { getDangerTypes, getControlMeasures } from "@/api/saferisk/riskpoint";

export default {
  props: {
    chooseMeasureDialog: {default: false},
    filterYHJB: {default: ""}
  },
  data() {
    return {
      dangerTypes: [],
      cmData: [],
      cmSelectionList: [],
      cmOption: {
        calcHeight: 30,
        tip: false,
        index: true,
        selection: true,
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: "检查单元",
            prop: "jcdy",
            align: "center"
          },
          {
            label: "隐患级别",
            prop: "yhjb",
            align: "center",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=control_measure_danger_level",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
          },
          {
            label: "管控措施",
            prop: "gkcs",
            align: "center"
          }
        ]
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化评估历史弹窗
    initData() {
      this.getDangerTypes();
      this.getControlMeasureList();
    },

    // 获取隐患级别列表
    getDangerTypes() {
      getDangerTypes().then(res => {
        this.dangerTypes = res.data.data;
      });
    },

    // 获取所有管控措施
    getControlMeasureList() {
      getControlMeasures().then(res => {
        this.cmData = res.data.data;
      });
    },

    // 勾选管控措施
    cmSelectionChange(list) {
      this.cmSelectionList = list;
    },

    // 选择管控措施
    chooseControlMeasure() {
      if(this.cmSelectionList.length === 0) {
        this.$message.warning("请选择管控措施!");
        return;
      }
      this.$emit('chooseControlMeasure', this.cmSelectionList);
    },

    // 取消选择管控措施
    cancelChooseControlMeasure() {
      this.$emit('cancelChooseControlMeasure');
    },

    // 关闭选择管控措施弹窗
    handleCloseChooseMeasure(done) {
      done();
      this.$emit('handleCloseChooseMeasure');
    },

  }
}
</script>